#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/3/26 15:53
# @Author : 闫峰
# @File    : multiprocessing.py
from multiprocessing import Pool
class Multiprocessing:

    def __init__(self,processes):
        self.processes= processes

    def run(self,func):
        pool = Pool(processes=self.processes)  # 创建进程池，指定进程数为4
        results = []
        for i in range(1000):
            result = pool.apply_async(func, args=(i,))  # 异步执行任务
            results.append(result)
        pool.close()  # 关闭进程池
        pool.join()  # 等待所有进程执行完毕
        for result in results:
            print(result.get())  # 获取任务执行结果



